from pika import BlockingConnection, ConnectionParameters
from pika.adapters.blocking_connection import BlockingChannel

from src.logger import Logger


class MessageQueue:
    channels: list[BlockingChannel]

    def __init__(self, host: str, port: int | str) -> None:
        params = ConnectionParameters(host, port, heartbeat=600)
        self.connection = BlockingConnection(params)
        self.channel = self.connection.channel()
        self.channel.basic_qos(prefetch_count=1)

    def start_consuming(self):
        try:
            Logger.Info("waiting for messages (CTRL+C)")
            self.channel.start_consuming()
        except KeyboardInterrupt:
            self.channel.stop_consuming()
            self.connection.close()
